package com.qf.business.student.valid;

import com.qf.business.student.input.StudentInput;
import com.qf.commons.web.valid.CustemValid;
import com.qf.commons.web.valid.KenValid;
import org.springframework.stereotype.Component;

import java.util.Calendar;
import java.util.Date;

/**
 * description: 生日和年龄的校验器
 * author: Ken
 * 公众号：Java架构栈
 */
@Component
public class MyValid implements KenValid<StudentInput> {

    @Override
    public boolean isValid(CustemValid custemValid, StudentInput value) {
        Integer age = value.getAge();
        Date birthday = value.getBirthday();

        if (age == null || birthday == null) return true;

        Calendar ca = Calendar.getInstance();
        ca.setTime(new Date());
        int nowYear = ca.get(Calendar.YEAR);

        ca.setTime(birthday);
        int birYear = ca.get(Calendar.YEAR);
        return nowYear - birYear == age;
    }
}
